﻿CREATE PROCEDURE [dbo].[proc_Orders_Getlist_Cus]
	(
		@CustomerId nvarchar(50)
	)
AS
begin
	
	;WITH list As(Select ROW_NUMBER() OVER (ORDER BY Orders.Id DESC)AS Row,

	Id,
	OId,
	OrderId,
	username,
	(select d.DName from Department d Where d.id=(select e.departmentid from employee e where e.username=Orders.username)) as DepartmentName,
	(select e.ename from employee e where e.username=Orders.username) as ename,
	CustomerId,
	isnull((Select CustomerName From Customer Where CustomerId=Orders.CustomerId),'...') As CustomerName,
	FixtureDate,
	Aggregateamount,
	Proceeds,
	FinanceFlag,
	MainProjectId,
	(select product.title from product where product.id=MainProjectId) AS MainProjectName
	From Orders Where  CustomerId=@CustomerId
	)
	
	Select * From list
end
	RETURN
